// new Promise((resolve, reject)=>{
//     setTimeout(function(){
//         resolve(10)
//     }, 1000)
// }).then((value)=>{
//     console.log("Promise then", value)
// }).catch((reason)=>{
//     console.log("Promise catch", reason)
// }).finally(()=>{
//     console.log("Promise finally")
// })

// Promise

const PENDING = "PENDING"
const FULLFILLED = "FULLFILLED"
const REJECTED = "REJECTED"
//1. Promise 是一个类
// 2. Promise 接受一个构造器函数作为参数，并执行该函数
function MyPromise(constructor){
    this.state = PENDING
    this.value = ""
    this.reason = ""
    
    const resolve = (value) =>{
       if(this.state === PENDING){
           this.state = FULLFILLED
           this.value = value
       }
    }

    const reject = (reason) => {
        if(this.state === PENDING){
            this.state = REJECTED
            this.reason = reason
        }
    }

    try {
        constructor(resolve, reject)
    } catch (error) {
        reject(error)
    }
   
}
// 三个原型上的方法
MyPromise.prototype.then = function(onFullfilled){
    onFullfilled(this.value)
}


const p = new MyPromise((resolve, reject)=>{
    resolve(123)
})
p.then((value)=>{
    console.log(value)
})




// // 函数作为参数传递
// function test(fun){
//     function inner(){
//         console.log(11)
//     }

//     var a = 111

//     fun()
// }

// test(function(){
//     console.log(a)
//     inner()
// })